package com.grandtech.insurance.common.oss;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class OssDownload {

    public static Boolean download(AliOssClientParamDO aliOssClientParamDO, String key, String savePath) {
        try {
            if (aliOssClientParamDO == null || key == null || savePath == null) {
                return false;
            }
            Integer ossType = OssType.type;
            if (ossType == 0) {
                AliOssClientUtils.downFile(aliOssClientParamDO, key, savePath);
            } else {
                String url = AliOssURL.url(aliOssClientParamDO, key);
                download(url, savePath);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String downloadUrl(AliOssClientParamDO aliOssClientParamDO, String key) {
        try {
            if (aliOssClientParamDO == null || key == null) {
                return "";
            }
            String url = AliOssURL.url(aliOssClientParamDO, key);
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 返回字节数组
     *
     * @param aliOssClientParamDO
     * @param key
     */
    public static byte[] download(AliOssClientParamDO aliOssClientParamDO, String key) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        InputStream is = null;
        byte[] returnByte = null;
        try {
            if (aliOssClientParamDO == null || key == null) {
                return null;
            }
            String urlStr = AliOssURL.url(aliOssClientParamDO, key);
            // 构造URL
            URL url = new URL(urlStr);
            // 打开连接
            URLConnection con = url.openConnection();
            // 设置请求超时时间
            con.setConnectTimeout(60 * 1000);
            // 输入流
            is = con.getInputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
            bos.flush();
            returnByte = bos.toByteArray();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return returnByte;
    }

    public static void download(String urlString, String savePath) throws Exception {
        InputStream is = null;
        OutputStream os = null;
        try {
            // 构造URL
            URL url = new URL(urlString);
            // 打开连接
            URLConnection con = url.openConnection();
            // 设置请求超时时间
            con.setConnectTimeout(60 * 1000);
            // 输入流
            is = con.getInputStream();

            // 数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            File sf = new File(savePath);
            //if (!sf.exists()) {
            //    sf.mkdirs();
            //}
            os = new FileOutputStream(sf.getPath());
            // 开始读取
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕，关闭所有链接
            if (os != null) {
                os.close();
            }
            if (is != null) {
                is.close();
            }
        }
    }

    public static void main(String[] args) {
        AliOssClientParamDO aliOssClientParamDO = new AliOssClientParamDO();
        aliOssClientParamDO.setEndpoint("https://oss-cn-zhangjiakou.aliyuncs.com");
        aliOssClientParamDO.setAccessKeyId("LTAIP816Jan0Nvjq");
        aliOssClientParamDO.setBucketName("ram-agriinsurance-test");
        aliOssClientParamDO.setAccessKeySecret(OssConstant.accessKeySecret);
        download(aliOssClientParamDO, "2020/01/10/bcf10eabfb1b4e32bf63f2f3b2cd2ca6/TDLZMX.pdf",
                "C:\\Users\\张建春\\Desktop\\tdlzmx.pdf");
    }

}
